package leetcode

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
	resHead := ListNode{0, nil}
	tailPtr := &resHead
	for list1 != nil && list2 != nil {
		if list1.Val > list2.Val {
			tailPtr.Next = list2
			list2 = list2.Next
		} else {
			tailPtr.Next = list1
			list1 = list1.Next
		}
		tailPtr = tailPtr.Next
	}
	if list1 != nil {
		tailPtr.Next = list1
	}
	if list2 != nil {
		tailPtr.Next = list2
	}
	return resHead.Next
}
